package com.thinkgem.jeesite.modules.sys.web;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.dao.TbOrderMapper;
import com.thinkgem.jeesite.modules.sys.dao.TbShopMapper;
import com.thinkgem.jeesite.modules.sys.entity.*;
import com.thinkgem.jeesite.modules.sys.service.*;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jyy on 2016/10/19 0019.
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/shopInStorehouse")
public class TbShopInStorehostController extends BaseController {
    @Autowired
    private TbShopInStorehostService tbShopInStorehostService;
    @Autowired
    private ProductService productService;
    @Autowired
    private TbOrderService tbOrderService;
    @Autowired
    private TbOrderProductService tbOrderProductService;
    @Autowired
    private TbShopService tbShopService;
    @Autowired
    private TbShopMapper tbShopMapper;
    @Autowired
    private TbOrderMapper tbOrderMapper;


    // 商品入库显示conactname
    @RequestMapping(value = "showShopInStorehouse")
    public String queryShopInStorehouse(TbOrder tbOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
        String uid = UserUtils.getUser().getId();
        TbShop shop = new TbShop();
        shop.setUid(uid);
        shop = tbShopService.selectShop(shop);
        tbOrder.setUid(shop.getId() + "");
        Page<TbOrder> userGardenStuffPage = tbShopInStorehostService.findUserReserveByPage(new Page<TbOrder>(request, response), tbOrder);
        model.addAttribute("tbOrder", tbOrder);
        model.addAttribute("userGardenStuffPage", userGardenStuffPage);
        if (userGardenStuffPage.getList().size() == 0) {
            model.addAttribute("message1", "无查询结果");
        }
        return "modules/sys/showShopInStorehostList";
    }

    // 进货历史
    @RequestMapping(value = "showShopHistory")
    public String showShopHistory(TbOrder tbOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
        String uid = UserUtils.getUser().getId();
        TbShop shop = new TbShop();
        shop.setUid(uid);
        shop = tbShopService.selectShop(shop);
        tbOrder.setUid(shop.getId() + "");
        tbOrder.setOrderState(new Byte("4"));
        Page<TbOrder> userGardenStuffPage = tbShopInStorehostService.findUserReserveByPage(new Page<TbOrder>(request, response), tbOrder);
        model.addAttribute("userGardenStuffPage", userGardenStuffPage);
        if (userGardenStuffPage.getList().size() == 0) {
            model.addAttribute("message1", "无查询结果");
        }
        return "modules/sys/showShopInStorehost";
    }

    // 入库商品订单信息
    @RequestMapping(value = "reserveInStorehouse/{orderId}")
    public String queryReserveGardenStuff(@PathVariable("orderId") Integer orderId, Model model) {
        TbOrder reserveTbOrder = tbOrderService.queryOrder(orderId);
        model.addAttribute("reserveTbOrder", reserveTbOrder);
        model.addAttribute("orderNo", reserveTbOrder.getOrderNo());
        List<TbOrderProduct> reserveTbOrderProductList = tbOrderProductService.queryTbOrderProducts(orderId);
        model.addAttribute("reserveTbOrderProductList", reserveTbOrderProductList);
        return "modules/sys/reserveInStorehouse";
    }

    // 入库商品订单信息
    @RequestMapping(value = "proInStorehouse/{orderId}")
    public String proInStorehouse(@PathVariable("orderId") Integer orderId, Model model) {
        TbOrder reserveTbOrder = tbOrderService.queryOrder(orderId);
        model.addAttribute("reserveTbOrder", reserveTbOrder);
        model.addAttribute("orderNo", reserveTbOrder.getOrderNo());
        List<TbOrderProduct> reserveTbOrderProductList = tbOrderProductService.queryTbOrderProducts(orderId);
        model.addAttribute("reserveTbOrderProductList", reserveTbOrderProductList);
        return "modules/sys/reserveInStorehouseList";
    }

    // 进货订单确认送达
    @RequestMapping(value = "updateStatus")
    public String updateStatus(HttpServletRequest request, Model model) {
        String orderNo = request.getParameter("orderNo");
        tbOrderService.updateStatus(orderNo);
        return "redirect:" + adminPath + "/sys/shopInStorehouse/showShopInStorehouse";
    }

    // 入库商品
    @RequestMapping(value = "inStorehouseUpdateState/{id}")
    public String editInStorehouseUpdateState(@PathVariable("id") Integer id, TbOrder tbOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
        //根据uid查询得到shop_id
        String uid = UserUtils.getUser().getId();
        int shopId = tbShopInStorehostService.selectShopIdByUid(uid);
//        String channel=request.getParameter("channel");
//        //根据订单id得到订单信息
//        TbOrder reserveTbOrder =  tbOrderService.queryOrder(id);
//        //根据订单id，得到订单商品的list集合
        List<TbOrderProduct> reserveTbOrderProductList = tbOrderProductService.queryTbOrderProducts(id);

        for (TbOrderProduct tbOrderProduct : reserveTbOrderProductList) {
            TbProduct tbProduct = productService.get(tbOrderProduct.getProductId());
            if (tbProduct == null || tbProduct.getReserveFlag().equals("3")) { //生活预订
                continue;
            }

            TbShopProduct tbShopProduct = new TbShopProduct(shopId,
                    tbOrderProduct.getProductId(),
                    tbOrderProduct.getPrice(),
                    tbOrderProduct.getQuantity(),
                    tbProduct.getSize(), tbProduct.getColor(), tbProduct.getIsUseMqWallet(), new Date(), new Date());

            if ("3".equals(tbProduct.getReserveFlag())) {
                tbShopProduct.setReserveFlag(new Byte("1"));
            } else {
                tbShopProduct.setReserveFlag(new Byte("0"));
            }
            tbShopProduct.setAttr(tbProduct.getAttr());
            tbShopProduct.setColor(tbProduct.getColor());
            Map map = new HashMap();
            map.put("shopId", shopId);
            map.put("productId", tbProduct.getId());
            List<TbShopProduct> tbShopProductList = tbShopInStorehostService.selectShopProductIsHave(map);
            if (tbShopProductList == null || tbShopProductList.size() == 0) {
                tbShopInStorehostService.addShopProduct(tbShopProduct);
            }else{
                tbShopInStorehostService.editShopProductStoreNum(tbShopProduct);
            }
        }

//        for (int i = 0; i < reserveTbOrderProductList.size(); i++) {
//            //根据订单商品对象中的product_id得到商品信息
//            TbProduct tbProduct = productService.get(reserveTbOrderProductList.get(i).getProductId());
//            //判断店铺商品表中是否存在改物品信息
//            if (!"3".equals(tbProduct.getReserveFlag())) {
//                Map map = new HashMap();
//                map.put("shopId", shopId);
//                map.put("productId", reserveTbOrderProductList.get(i).getProductId());
//                List<TbShopProduct> tbShopProductList = tbShopInStorehostService.selectShopProductIsHave(map);
//                //店铺商品对象
//
//                if (reserveTbOrderProductList.get(i) != null && tbProduct != null) {
//                    //如果shopProductId有值，执行修改，没有执行新增
//                    for (TbShopProduct list : tbShopProductList) {
//                        TbShopProduct shopProduct = new TbShopProduct(shopId, reserveTbOrderProductList.get(i).getProductId(), reserveTbOrderProductList.get(i).getPrice(), reserveTbOrderProductList.get(i).getQuantity(), tbProduct.getSize(), tbProduct.getColor(), tbProduct.getIsUseMqWallet(), new Date(), new Date());
//                        if ("3".equals(tbProduct.getReserveFlag())) {
//                            shopProduct.setReserveFlag(new Byte("1"));
//                        } else {
//                            shopProduct.setReserveFlag(new Byte("0"));
//                        }
//                        shopProduct.setAttr(list.getAttr());
//                        if (shopProduct.getProductId().equals(reserveTbOrderProductList.get(i).getProductId()) && shopProduct.getAttr().equals(reserveTbOrderProductList.get(i).getAttr())) {
//                            tbShopInStorehostService.editShopProductStoreNum(shopProduct);
//                            reserveTbOrderProductList.remove(i);
//                        }
//                    }
//                }
//            }
//        }
//        for (TbOrderProduct product : reserveTbOrderProductList) {
//            //根据订单商品对象中的product_id得到商品信息
//            TbProduct tbProduct = productService.get(product.getProductId());
//            //判断店铺商品表中是否存在改物品信息
//            if (!"3".equals(tbProduct.getReserveFlag())) {
//                Map map = new HashMap();
//                map.put("shopId", shopId);
//                map.put("productId", product.getProductId());
//                List<TbShopProduct> tbShopProductList = tbShopInStorehostService.selectShopProductIsHave(map);
//                //店铺商品对象
//                if (product != null && tbProduct != null) {
//                    TbShopProduct tbShopProduct = new TbShopProduct(shopId, product.getProductId(), product.getPrice(), product.getQuantity(), tbProduct.getSize(), tbProduct.getColor(), tbProduct.getIsUseMqWallet(), new Date(), new Date());
//                    if ("3".equals(tbProduct.getReserveFlag())) {
//                        tbShopProduct.setReserveFlag(new Byte("1"));
//                    } else {
//                        tbShopProduct.setReserveFlag(new Byte("0"));
//                    }
//                    tbShopProduct.setAttr(product.getAttr());
//                    tbShopProduct.setColor(product.getAttr());
//                    tbShopInStorehostService.addShopProduct(tbShopProduct);
//                }
//            }
//        }
        tbShopInStorehostService.editOrderState(id);
        model.addAttribute("message", "商品成功入库");
        return "forward:" + adminPath + "/sys/shopInStorehouse/showShopHistory";
    }

    // 购物订单状态的修改
    @RequestMapping(value = "deleteOrder/{id}")
    public String deleteOrder(@PathVariable("id") Integer id, HttpServletRequest request) {
        String type = request.getParameter("type");
        String channel = request.getParameter("channel");
        if ("1".equals(type)) {
            User user = UserUtils.getUser();
            String uid = user.getId();
            TbShop shop = new TbShop();
            shop.setUid(uid);
            shop = tbShopMapper.selectByUid(shop);
            //修改该店铺下的预定订单 查詢該訂單的productId 衹有一個
            TbOrderProduct tbOrderProduct = tbOrderProductService.queryTbOrderProductByOrderId(id);
            if (null != tbOrderProduct) {
                tbOrderService.updatePreCancelOrder(tbOrderProduct, id);
            }
            if ("2".equals(channel)) {
                return "redirect:" + adminPath + "/sys/tbShopController/reverseProductOrderListForSea";
            } else {
                return "redirect:" + adminPath + "/sys/tbShopController/reverseProductOrderList";
            }

        }
        return "redirect:" + adminPath + "/sys/shopInStorehouse/showShopInStorehouse";
    }
}
